package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.service.UserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    ResultVo resultVo = null;
    ObjectMapper objectMapper = new ObjectMapper();
    UserService userService= new UserServiceImpl();
     //登录
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");
        HttpSession session = req.getSession();
        String verCode = (String) session.getAttribute("verCode");
        //判断验证码是否正确
        if (imageCode.equalsIgnoreCase(verCode)){
            User user = userService.login(username, password);
            //判断是否查到数据
            if (user!=null){
                //判断是不是管理人员
                if (user.getRole()==1){
                    session.setAttribute("user",user);
                    resultVo = new ResultVo(4,"登录成功..欢迎光临",user);

                }else {
                    resultVo = new ResultVo(3,"您不是管理员",null);
                }

            }else {
                resultVo = new ResultVo(2,"用户名或密码错误",null);
            }
        }else {
            resultVo = new ResultVo(1,"验证码错误",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

      // 将session的数据取出响应到web让其他页面也能获取到数据
   public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       HttpSession session = req.getSession();
       User user = (User) session.getAttribute("user");
       resp.getWriter().print(objectMapper.writeValueAsString(user));
   }

       //  退出 ，要将session的数据删除
    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        // 通过名字删除session的数据
        session.removeAttribute("user");
        //将状态码写入对象
        resultVo = new ResultVo(200,"退出成功",null);
        //将状态码响应到web
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

        //添加数据
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //创建一个user对象存数据
        User user = new User();
        //使用BeanUtils将要添加的数据存入对象中
        BeanUtils.populate(user,req.getParameterMap());
         //判断返回值
        boolean b = userService.add(user);
        if (b){
            // 将状态码和信息写入对象
            resultVo = new ResultVo(200,"添加成功",null);
        }else {
            //  将状态码和信息写入对象
            resultVo = new ResultVo(500,"添加失败",null);
        }
           // 将状态码和信息响应到web
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

      //  分页
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //  获取前端传过来的参数
        String search = req.getParameter("search");
        int  currentPage = Integer.parseInt( req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
         //根据参数查询所需的数据
        PageUtils<User> pageUtils = userService.search(search,currentPage,pageSize);
        resultVo = new ResultVo(200,"查询成功",pageUtils);
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }


    //     修改用户信息
   public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = new User();
//        使用BeanUtils封装请求数据到实体类中
        BeanUtils.populate(user,req.getParameterMap());
        boolean b = userService.update(user);
        if (b){
            resultVo = new ResultVo(200,"修改成功",null);
        }else {
            resultVo = new ResultVo(500,"修改失败",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }


     //   删除用户信息
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取前端传入的数据 名为UID有多个所有用数组接收
        String[] uids = req.getParameterValues("uid");
        boolean b = userService.del(uids);
        if (b){
            resultVo = new ResultVo(200,"删除成功",null);
        }else {
            resultVo = new ResultVo(500,"删除失败",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }
}

